#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "IniHandle/IniHandleApi.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    qWarning("MainWindow start");


    if(InitIniHandler("./etc/config.ini")==false)
      qDebug()<<"ini file read failed!";
    else
      qDebug()<<"ini file read succeeded!";


    update_interval_ms = GetIntegerKeyIni("Video", "update_interval_ms", 100); 

    qDebug()<<"update_interval_ms:"<<update_interval_ms;


    picture_input_dir = GetStringValueKeyIni("PictureInput", "picture_input_dir", "../picture"); 

    ui->setupUi(this);
    uavFlag = false;
    playFlag = false;
    updateCount = 0;

    videoTimer = new QTimer(this);
    msgTimer = new QTimer(this);
    connect(msgTimer,SIGNAL(timeout()),this,SLOT(msgUpdate()));
    connect(videoTimer,SIGNAL(timeout()),this,SLOT(imgUpdate()));

    ui->openButton->hide();
    dir = new QDir(picture_input_dir.c_str());
    if(!dir->exists())
    {
      qWarning("cannot open dir");
    }
    else qWarning("pic dir open!");



    int wait_time = GetIntegerKeyIni("PicCache", "piccache_wait_time", 20); 

    picCache = new PicCache(dir, wait_time);
    picCache->start();

    red.load(":/new/prefix1/img/light-red.png");
    gray.load(":/new/prefix1/img/light-gray.png");
    arrow.load(":/new/prefix1/img/arrow.png");

    numFont.setBold(1);
    numFont.setPixelSize(20);

    ui->detailList->addItem("视频分辨率");
    ui->detailList->addItem("0x0");//1
    ui->detailList->addItem(" ");
    ui->detailList->addItem("当前总帧数");
    ui->detailList->addItem("0");//4
    ui->detailList->addItem(" ");
    ui->detailList->addItem("已缓存帧数");
    ui->detailList->addItem("0");//7
    ui->detailList->addItem(" ");
    ui->detailList->addItem("当前帧数");
    ui->detailList->addItem("0");//10
    ui->detailList->addItem(" ");

    std::string tmpstr= std::to_string(1000.0/(double)update_interval_ms);
    std::string str_update_interval_ms  = tmpstr.substr(0, 7);

    ui->detailList->addItem("当前帧率");
    ui->detailList->addItem(str_update_interval_ms.c_str());//13 update_interval_ms

    ui->detailList->item(1)->setBackgroundColor(Qt::gray);
    ui->detailList->item(4)->setBackgroundColor(Qt::gray);
    ui->detailList->item(7)->setBackgroundColor(Qt::gray);
    ui->detailList->item(10)->setBackgroundColor(Qt::gray);
    ui->detailList->item(13)->setBackgroundColor(Qt::gray);

    ui->detailList->item(1)->setIcon(QIcon(arrow));
    ui->detailList->item(4)->setIcon(QIcon(arrow));
    ui->detailList->item(7)->setIcon(QIcon(arrow));
    ui->detailList->item(10)->setIcon(QIcon(arrow));
    ui->detailList->item(13)->setIcon(QIcon(arrow));

    ui->detailList->item(1)->setFont(numFont);
    ui->detailList->item(4)->setFont(numFont);
    ui->detailList->item(7)->setFont(numFont);
    ui->detailList->item(10)->setFont(numFont);
    ui->detailList->item(13)->setFont(numFont);

    ui->flagLabel->setPixmap(gray);
    msgTimer->start(100);
    ui->imgLabel->setAlignment(Qt::AlignCenter);

    qWarning("MainWindow start ok");
}

MainWindow::~MainWindow()
{
    qWarning("MainWindow stop");
    delete ui;

    UninitIniHandle();

}

void MainWindow::imgUpdate()
{
    //qWarning("imgUpdate");

    if(updateCount < picCache->picList->count())
    {
        ui->imgLabel->setPixmap(picCache->picList->at(updateCount).pixmap); //刷新图像
        updateCount++;//刷新图像的编号，和list中的编号一一对应
                
        //tODO: 若发现无人机，则在文件中寻找坐标信息
        if(picCache->picList->at(updateCount -1).uavFlag == true)
        {
            if(uavFlag == false)
            {
                //QApplication::beep();
                QSound::play(":/new/prefix1/uav.wav");
            }//从无到有，报警

            uavFlag = true;

            ui->flagLabel->setPixmap(red);
            for(int i=0 ; i< picCache->picList->at(updateCount -1).uavNum ; i++)
            {
                //matStr = in.readLine();
                //                if(in.atEnd() == 1)
                //                    qDebug() << "at end!";
                //                qDebug() << i<<":"<< matStr;
            }
        }
        else
        {
            uavFlag = false;
            ui->flagLabel->setPixmap(gray);
            matStr = "0 0 0 0";
        }

    }
    //else qDebug()<<"缓存慢于界面刷新";

}

void MainWindow::msgUpdate()
{
  //qWarning("msgUpdate");
    if(picCache->picList->count() != 0)
        ui->timeSlider->setValue(100.0*updateCount/picCache->picList->count());

    ui->detailList->item(1)->setText(QString::number(picCache->picWidth) + QString("x") + QString::number(picCache->picHeight));
    ui->detailList->item(4)->setText(QString::number(picCache->oldFileCount));
    ui->detailList->item(7)->setText(QString::number(picCache->picList->count()));
    ui->detailList->item(10)->setText(QString::number(updateCount));
    ui->timeLabel->setText(QString::number(updateCount));
}

void MainWindow::on_playButton_clicked()
{
  qWarning("on_playButton_clicked()");
    playFlag = !playFlag;
    if(playFlag)
    {
        ui->playButton->setIcon(QIcon(":/new/prefix1/img/play.png"));
        videoTimer->stop();
    }
    else
    {
        ui->playButton->setIcon(QIcon(":/new/prefix1/img/pause.png"));
        videoTimer->start(update_interval_ms);
    }
}


void MainWindow::on_timeSlider_sliderMoved(int position)
{
    qWarning("on_timeSlider_sliderMoved");
    updateCount = picCache->picList->count()/100.0*position;
    ui->timeLabel->setText(QString::number(updateCount));
    ui->playButton->setIcon(QIcon(":/new/prefix1/img/play.png"));
    videoTimer->stop();
    imgUpdate();
}

void MainWindow::on_detailButton_clicked()
{
  qWarning("on_detailButton_clicked()");
    static bool flag = false;
    if(flag)
        ui->detailButton->setIcon(QIcon(":/new/prefix1/img/details.png"));
    else
        ui->detailButton->setIcon(QIcon(":/new/prefix1/img/details_gray.png"));
    flag = !flag;
}

void MainWindow::on_openButton_clicked()
{
  qWarning("on_openButton_clicked()");
    QFileDialog fileDialog;
    dirStr = fileDialog.getExistingDirectory();
    qDebug() << dirStr;
}
